$(document).ready(function() { 
	    // 判断是否重复添加了老师和课程的组合
		function checkCourseTeacherIsSelected(obj){
			for(var i=0;i<$("#teacherCourseClasses").get(0).options.length;i++)  {   
				if($("#teacherCourseClasses").get(0).options[i].value == obj){
					return true
				}
			}		
		}

		// 判断是否已经添加过某一课程
		function checkCourseIsSelected(obj){
			for(var i=0;i<$("#teacherCourseClasses").get(0).options.length;i++)  {   
				if($("#teacherCourseClasses").get(0).options[i].course == obj){
					return true
				}
			}		
		}

		$("#teacherCourseClasses").live("dblclick", function(){
			$("#teacherCourseClasses option[selected]").remove();
		});

		$("#clearAllOption").click(function (){
			$("#teacherCourseClasses").get(0).options.length = 0;   
		});

		// 点击教师姓名，进入已选代课老师行
		$('.selectCourseTeacher').live("click", function() {
			if(checkCourseIsSelected($(this).attr("course"))){
				alert("已经添加过"+$(this).attr("course")+"课程的老师！")
			}else{
				if(checkCourseTeacherIsSelected($(this).attr("course")+","+$(this).attr("teacherID"))){
					alert("不要重复添加老师")
				}else{
					$("#teacherCourseClasses").append("<option value='"+$(this).attr("course")+","+$(this).attr("teacherID")+"' course = '"+$(this).attr("course")+"' class='selectedOption'>"+$(this).attr("teacherName")+"（"+$(this).attr("course")+"）</option>");  
				}
			}
		});
 

		// 根据学科找到对应的老师
		$(".courseSelectTeacher").click(function() {
			var selectCourse = $.trim($(this).attr("code"))
			$("#teacherCourseSelect").html("")
			$.post("/cloud/classesAjax/getCourseTeacher", {time:new Date().getTime(),courseName:$.trim($(this).attr("code")),school:"1"}, function(data) {
					 if( data.success == true ){
						$.each( data.message, function(i, n){
						  $("#teacherCourseSelect").html($("#teacherCourseSelect").html()
							  +"<span id=''>"
							  +"<a href='javascript:' class='selectCourseTeacher' teacherName="+n.name+" teacherID="+n.id+" course="+selectCourse+">"
							  +n.name
							  +"</a>"
							  +"</span>&nbsp;&nbsp;&nbsp;")						   
						});
					}else{
						  alert(data.message)
					}
				}, 'json');			
		});   

		$("#classesMgrSelectTeacher").submit(function() { 
				for(var i=0;i<$("#teacherCourseClasses").get(0).options.length;i++)  { 
					$("#selectedTeacher").val($("#selectedTeacher").val()+"|"+$("#teacherCourseClasses").get(0).options[i].value);
				}
				return true;
		});
  });

  